package JUC.Thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @program: jvm
 * @description   java高并发程序设计
 * @author: tkk fendoukaoziji
 * @create: 2019-03-13 13:47
 **/
public class SemapDemo implements  Runnable{
    final Semaphore semp=new Semaphore(5);
    @Override
    public void run() {
        try {
            semp.acquire();
            //模拟操作
            Thread.sleep(3000);
            System.out.println(Thread.currentThread().getId()+":Done!");
            semp.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        ExecutorService exec = Executors.newFixedThreadPool(20);
        final SemapDemo demo = new SemapDemo();
        for (int i = 0; i < 20; i++) {
            exec.submit(demo);
        }
    }
}
